<?php
/**
 * @author yzs1013@163.com
 * filename zqnews.class.php
 */


require_once(APP_PATH . "Common/yzs1013_pldr.php");


class zqnews
{
	public $url="";
	public $nr="";
	public $jj="";
	public $pic="";
	private $tempnr;
	protected $parentpath="";
    
	public function zqnews($url){
		$tempnr=@file_get_contents($url);
		if($tempnr==""){
			include_once("page.class.php");
			$p=new page($url);
		    $tempnr= $p->html;
		}
		$tempnr=dquanurl($tempnr,$url);

		if(preg_match("/<meta[^<>]*charset[^<>]*(GB2312|gbk)[^<>]*>/Uisx",$tempnr)){
			$tempnr=iconv("GB2312","UTF-8//IGNORE",$tempnr);
		}
		$this->url=$url;
		$this->nr=$tempnr;
        return $this->nr;
	}


	public function getbt(){
		preg_match_all("/<title>(.*)<\/title>/Uisx",$this->nr,$nn);
		$subject=$nn[1][0];      
		$subject=strip_jscss($subject);
        
		$subject=preg_replace("/\"/","\\\"",strip_tags($subject,"<img><p><br><b><strong><td><table><tr><ul><li><span>"));
		$subject=preg_replace("/(\r|\n)+|$/m","",trim($subject));
		$subject=preg_replace("/\&nbsp\;/Uisx"," ",$subject);
		$subject=preg_replace("/(_|\-|\-|\|\—).*$/Uisx","",$subject);
		$subject=preg_replace("/(_|\-|\-|\|\s)[\x{4e00}-\x{9fa5}A-Za-z0-9_]*(网|社区|论坛).*$/Uuisx","",$subject);
		//$subject=preg_replace("/((\s| |".g(" ")."|\-)[\x{4e00}-\x{9fa5}A-Za-z0-9_]+??).*$/isxu","",$subject);//utf8??      
		//$subject=preg_replace("/((\s| |\-|\|)[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+??).*$/isx","",$subject);	//gbk
	    return $subject;
	}



    public function getbbs(){
		$tempnr=$this->nr;

        if(preg_match("/discuz/Uisx",$tempnr)){
			preg_match_all("/<div\s*class=\"authi\"[^<>]*>.*<a[^<>]*uid=[^<>]*>([^<>]*)<\/a>.*<td\s*class=\"t_f\"\s*id=\"postmessage_[0-9]+\">(.*)<\/td><\/tr><\/table>/Uisx",$tempnr,$nn);
			$userid=$nn[1][0];
			preg_match_all("/<div\s*class=\"authi\"[^<>]*>.*<a[^<>]*uid=[^<>]*>{$userid}<\/a>.*<td\s*class=\"t_f\"\s*id=\"postmessage_[0-9]+\">(.*)<\/td><\/tr><\/table>/Uisx",$tempnr,$nn);
			for($i=0;$i<count($nn[1]);$i++){
			$textarea.=$nn[1][$i]."\r\n";
			break;
			}
		}

		if(preg_match("/tianya/Uisx",$tempnr)){
			//preg_match_all("/<div[^<>]*class=\"post\"[^<>]*>(.*)<\/div>/Uisx",$tempnr,$nn);
			//$userid=$nn[1][0];
			preg_match_all("/<div[^<>]*class=\"post\"[^<>]*>(.*)<\/div>/Uisx",$tempnr,$nn);
			for($i=0;$i<count($nn[1]);$i++){
			$textarea.=$nn[1][$i]."\r\n";
			break;
			}
		}

		$rs=getre(host($this->url),'bbs');
		if($rs[re]!=""){
				   $cc=explode("|||",$rs[re]);
				   for($i=0;$i<count($cc);$i++){
					   if(trim($cc[$i])=="") continue;
					   preg_match_all("/".trim($cc[$i])."/Uisx",$tempnr,$nn);
					   $textarea=trim($nn[1][0]);
					   if($textarea!="")  break;
				   }
		}




		$textarea=strip_jscss($textarea);
	
		$textarea=preg_replace("/\"/","\\\"",strip_tags($textarea,"<img><p><br><b><strong><td><table><tr><ul><li><span>"));
		//$textarea=preg_replace("/(\r|\n)+|$/m","\\r\\n",trim($textarea));
	
		preg_match_all("/<img([^<>]*)>/Uisx",$textarea,$nn00);
		for($i=0;$i<count($nn00[1]);$i++){
			 preg_match_all("/file=\\\?\"([^<>]*)\\\??\"/Uisx",$nn00[0][$i],$nn3);$src=$nn3[1][0];
			 if($src==""){
			 preg_match_all("/src=\\\?\"([^<>]*)\\\??\"/Uisx",$nn00[0][$i],$nn2);$src=$nn2[1][0];
			 }
			 //$document=str_replace($nn[0][$i],"[img]/source/plugin/yzs1013_pldr/getimg.php?url=".$src."[/img]",$document);
			 $textarea=str_replace($nn00[0][$i],"[img]".$src."[/img]",$textarea);
		}
			
		$textarea=preg_replace("/本帖.*[0-9\-\s\:]编辑/Uisx","",$textarea);
		$textarea=preg_replace("/<span[^<>]*style=\\\?\"display\:none\\\?\"[^<>]*>.*<\/span>/Uisx","",trim($textarea));
		$textarea=preg_replace("/<span[^<>]*>|<\/\s*span>/Uisx","",$textarea);
		$textarea=preg_replace("/\(微博\)/m","",trim($textarea));
		if($rs['delete']!=""){
		   $dd=explode("|||",$rs['delete']);
		   for($i=0;$i<count($dd);$i++){
			   if(trim($dd[$i])=="") continue;
			   $textarea=preg_replace("/".trim(u($dd[$i]))."/Uisx","",$textarea);
		   }
		}
		if($rs['exec']!=""){
			if($rs[re]!=""){
				$ddd=explode("&&&",$rs[re]);
				if(count($ddd)>1){
					$tempnr=$this->nr;
					for($d=0;$d<count($ddd)-1;$d++){
						 preg_match_all("/".trim($ddd[$d])."/Uisx",$tempnr,$nn);
						 $tempnr=$nn[1][0];
					}
					preg_match_all("/".$ddd[count($ddd)-1]."/Uisx",$tempnr,$nn);
				}else{
					preg_match_all("/".$rs[re]."/Uisx",$tempnr,$nn);
				}
			}
			eval($rs['exec']);
		}
		$textarea=htmltotxt($textarea);                
		$textarea=droppagetablediv($textarea);//删除第页行

        return $textarea;
	}



	public function getflv(){
		switch(host($this->url)){
		case "tudou.com":
				preg_match_all("/tudou\.com\/(.*)\//Uisx",$this->url,$nn);
				switch($nn[1][0]){
					case "albumplay":
					$a="a";break;
					case "programs":
					$a="v";break;
					default:
					$a="l";					
				}
				preg_match_all("/(?:l|i|a)id\s*=\s*([0-9]*)/isx",$this->nr,$nn);$lid=$nn[1][0];
				preg_match_all("/\,(?:l|i|a)code\s*=\s*'([^']*)'/Uisx",$this->nr,$nn);$lcode=$nn[1][0];
				preg_match_all("/\,adesc\:\"([^<>]*)\"/Uisx",$this->nr,$nn);$jj=$this->jj=clmh($nn[1][0]);
				preg_match_all("/\,pic\:\"([^<>]*)\"/Uisx",$this->nr,$nn);$pic=$this->pic=clmh($nn[1][0]);
				if($pic!="") $picimg="<img src=\"{$pic}\">";
				$textarea="[media=swf,500,375]http://www.tudou.com/{$a}/{$lcode}/&resourceId=0_05_05_99&iid={$lid}&bid=05/v.swf[/media]";
				$textarea=clmh($textarea);
				break;
		case "youku.com":
				preg_match_all("/id_(.*)\.html/Uisx",$this->url,$nn);$id=$nn[1][0];
				preg_match_all("/\,(?:l|i|a)code\s*=\s*'([^']*)'/Uisx",$this->nr,$nn);$lcode=$nn[1][0];
				preg_match_all("/\,adesc\:\"([^<>]*)\"/Uisx",$this->nr,$nn);$jj=$this->jj=clmh($nn[1][0]);
				preg_match_all("/<li\s*class=\"v_thumb\"><img\s*src=\"([^<>]*)\"/Uisx",$this->nr,$nn);$pic=$this->pic=clmh($nn[1][0]);
				if($pic!="") $picimg="<img src=\"{$pic}\">";
				$textarea="[media=swf,500,375]http://player.youku.com/player.php/sid/{$id}/v.swf[/media]";
				$textarea=clmh($textarea);
				break;
		}
      return $textarea;
	}

	public function getnr(){
		
		switch(host($this->url)){
		case "tudou.com":
				preg_match_all("/tudou\.com\/(.*)\//Uisx",$this->url,$nn);
				switch($nn[1][0]){
					case "albumplay":
					$a="a";break;
					case "programs":
					$a="v";break;
					default:
					$a="l";					
				}
				preg_match_all("/(?:l|i|a)id\s*=\s*([0-9]*)/isx",$this->nr,$nn);$lid=$nn[1][0];
				preg_match_all("/\,(?:l|i|a)code\s*=\s*'([^']*)'/Uisx",$this->nr,$nn);$lcode=$nn[1][0];
				preg_match_all("/\,adesc\:\"([^<>]*)\"/Uisx",$this->nr,$nn);$jj=$this->jj=clmh($nn[1][0]);
				preg_match_all("/\,pic\:\"([^<>]*)\"/Uisx",$this->nr,$nn);$pic=$this->pic=clmh($nn[1][0]);
				if($pic!="") $picimg="<img src=\"{$pic}\">";
				$textarea="[media=swf,500,375]http://www.tudou.com/{$a}/{$lcode}/&resourceId=0_05_05_99&iid={$lid}&bid=05/v.swf[/media]";
				$textarea=clmh($textarea);
				break;
		case "youku.com":
				preg_match_all("/id_(.*)\.html/Uisx",$this->url,$nn);$id=$nn[1][0];
				preg_match_all("/\,(?:l|i|a)code\s*=\s*'([^']*)'/Uisx",$this->nr,$nn);$lcode=$nn[1][0];
				preg_match_all("/\,adesc\:\"([^<>]*)\"/Uisx",$this->nr,$nn);$jj=$this->jj=clmh($nn[1][0]);
				preg_match_all("/<li\s*class=\"v_thumb\"><img\s*src=\"([^<>]*)\"/Uisx",$this->nr,$nn);$pic=$this->pic=clmh($nn[1][0]);
				if($pic!="") $picimg="<img src=\"{$pic}\">";
				$textarea="[media=swf,500,375]http://player.youku.com/player.php/sid/{$id}/v.swf[/media]";
				$textarea=clmh($textarea);
				break;
		default:
		        $tempnr=$this->nr;
				preg_match_all("/<div[^<>]*>((?:.(?!<div))*)<\/div>/Uisx",$tempnr,$nn);
				$m=0;
                $rs=getre(host($this->url),'nr');
				if($rs[re]!=""){
				   $cc=explode("|||",$rs[re]);
				   for($i=0;$i<count($cc);$i++){
					   if(trim($cc[$i])=="") continue;
					   preg_match_all("/".trim($cc[$i])."/Uisx",$tempnr,$nn);
					   $textarea=trim($nn[1][0]);
					   if($textarea!="")  break;
				   }
				}

				if(trim($textarea)==""){
					do{
						preg_match_all("/<div[^<>]*>((?:.(?!<div))*)<\/div>/Uisx",$tempnr,$nn);
						for($i=0;$i<count($nn[1]);$i++){
							if(preg_match_all("/(。|，|<img[^<>]*width=\"[3-9][0-9][0-9]\"|<P\s*align=center>\s*<img)/Uisx",$nn[1][$i],$mm)){
								
								$cl=0;
								$havejuhao=0;
								for($j=0;$j<count($mm[1]);$j++){
								   //echo "\r\n[".trim(substr($mm[1][$j],0,4))."]\r\n";
								   switch(trim(substr(strtolower($mm[1][$j]),0,4))){
									 case "。":
										 $cl=$cl+2;
										 $havejuhao++;
										 break;
									 case "<img":
									 case "<p a":
										 $cl=$cl+6;break;
									 default:
										 $cl=$cl+1;break;
								   }
			
								}
								if($havejuhao>0){
									$sl[]=$cl;
									$mbnr[]=$nn[1][$i];
								}
								//echo "\r\n-------------div_{$cl}----------------\r\n".$nn[1][$i];
							}
							$tempnr=str_replace($nn[0][$i],"",$tempnr);
						}
					//echo $m."-".count($nn[1])."\r\n";exit;
					$m++;
					}while(count($nn[1])>1&&$m<10);

					//print_r($sl);
					//echo "---".my_array_max($sl)."\r\n";
					$textarea=$mbnr[my_array_max($sl)];
					//print_r($mbnr);

					preg_match_all("/<td[^<>]*>((?:.(?!<td))*)<\/td>/Uisx",$this->nr,$nn2);
					for($i=0;$i<count($nn2[1]);$i++){
						if(preg_match_all("/(。|，|<img[^<>]*width=\"[3-9][0-9][0-9]\")/Uisx",$nn2[1][$i],$mm)){
							
							$c2=0;
							for($j=0;$j<count($mm[1]);$j++){
							   switch(trim(substr(strtolower($mm[1][$j]),0,4))){
								 case "。":
									 $c2=$c2+2;break;
								 case "<img":
									 $c2=$c2+6;break;
								 default:
									 $c2=$c2+1;break;
							   }
							}						
							$sl2[]=$c2;
							$mbnr2[]=$nn2[1][$i];
							//echo "\r\n-------------td[".$c2."]----------------\r\n".$nn2[1][$i];
						}
					}


					if(my_array_max($sl2)>my_array_max($sl)) $textarea=$mbnr2[my_array_max($sl2)];
				}

				$textarea=strip_jscss($textarea);
			
				$textarea=preg_replace("/\"/","\\\"",strip_tags($textarea,"<img><p><br><b><strong><td><table><tr><ul><li><span>"));
				//$textarea=preg_replace("/(\r|\n)+|$/m","\\r\\n",trim($textarea));
			
				preg_match_all("/<img([^<>]*)>/Uisx",$textarea,$nn00);
				
				for($i=0;$i<count($nn00[1]);$i++){
					
					 preg_match_all("/file\s*\=\s*\\\?\"([^<>]*)\\\??\"/Uisx",$nn00[0][$i],$nn3);$src=$nn3[1][0];
					 if($src==""){
					 preg_match_all("/src\s*\=\s*\\\?\"([^<>]*)\\\??\"/Uisx",$nn00[0][$i],$nn2);$src=$nn2[1][0];
					 }
					 //$document=str_replace($nn[0][$i],"[img]/source/plugin/yzs1013_pldr/getimg.php?url=".$src."[/img]",$document);
					 $textarea=str_replace($nn00[0][$i],"[img]".$src."[/img]",$textarea);
				}
					
			    $textarea=preg_replace("/本帖.*[0-9\-\s\:]编辑/Uisx","",$textarea);
				$textarea=preg_replace("/<span[^<>]*style=\\\?\"display\:none\\\?\"[^<>]*>.*<\/span>/Uisx","",trim($textarea));
				$textarea=preg_replace("/<span[^<>]*>|<\/\s*span>/Uisx","",$textarea);
				$textarea=preg_replace("/\(微博\)/m","",trim($textarea));
				if($rs['delete']!=""){
				   $dd=explode("|||",$rs['delete']);
				   for($i=0;$i<count($dd);$i++){
					   if(trim($dd[$i])=="") continue;
					   $textarea=preg_replace("/".trim(u($dd[$i]))."/Uisx","",$textarea);
				   }
				}
				if($rs['exec']!=""){
					if($rs[re]!=""){
						$ddd=explode("&&&",$rs[re]);
						if(count($ddd)>1){
						    $tempnr=$this->nr;
							for($d=0;$d<count($ddd)-1;$d++){
								 preg_match_all("/".trim($ddd[$d])."/Uisx",$tempnr,$nn);
								 $tempnr=$nn[1][0];
							}
							preg_match_all("/".$ddd[count($ddd)-1]."/Uisx",$tempnr,$nn);
						}else{
							preg_match_all("/".$rs[re]."/Uisx",$tempnr,$nn);
						}
					}
					eval($rs['exec']);
				}
				$textarea=htmltotxt($textarea);                
				$textarea=droppagetablediv($textarea);//删除第页行
		}
        
		return $textarea;
	}
	
	
    
	//静态方法
	static public function usefunc($unitid,$unittype_id,$unitname){
	   return $result;
	}

	public function zq($re){
      global $tempnr;
	  preg_match_all("/".trim($re)."/Uisx",$tempnr,$nn);
	  $return=$nn[1][0];
	  return $return;
	}

}
?>